﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using Htss.Platform.Web;
using Htss.Platform.Core;

namespace DreamBox.Web.Box.posts
{
    public class replyBox:ControllerBox
    {
        public ActionResult Load()
        {
            DAL.Posts.PostsClass pc = new DAL.Posts.PostsClass();
            int GID = Htss.Platform.Core.Transform.GetInt(Session["GID"].ToString(), 0);
            int PostsID = Htss.Platform.Core.Transform.GetInt(RequestContext.RouteData.Values["id"].ToString(), 0);
            if (!pc.IsReply(GID, PostsID))
            {
                BLL.Message.Jump(this, "您没有回复此文章的权限!", "/", "0");
            }
            if (Request.RequestType == "POST")
            {
                if (Verification())
                {

                    Model.PostsReply m = new Model.PostsReply();
                    //
                    m.UserID = int.Parse(Session["UserID"].ToString());
                    m.PostsID = PostsID;
                    m.Contents = Htss.Platform.Core.Text.ReplaceHtml(Request.Form["Contents"]);
                    if (m.PostsID > 0)
                    {
                        DAL.Posts.Reply posts = new DAL.Posts.Reply();
                        //
                        string contents = "";
                        if (Request.QueryString["posts"] != null)
                        {
                            int ReplyID = Htss.Platform.Core.Transform.GetInt(Request.QueryString["reply"], 0);

                            if (ReplyID > 0)
                            {
                                DAL.PSelect ps = new DAL.PSelect();
                                //加载回复其他用户的帖子
                                DataTable postsDt = ps.Select("dbox_v_replyposts", "UserID,UserName,PostsID,Contents", "ReplyID=" + ReplyID);
                                contents = "<blockquote class='blockquote'><a href='#'>" +
                                        postsDt.Rows[0]["UserName"].ToString() + "</a>：" +
                                            postsDt.Rows[0]["Contents"].ToString() + "</blockquote>";
                            }
                        }
                        m.Contents = contents + m.Contents;
                        if (posts.Add(m))
                        {
                            //更新回帖数
                            posts.UpdatePostsCount(m.PostsID.Value);
                            BLL.Message.Jump(this, "回复成功!", "/posts/read/" + m.PostsID, "1");
                        }
                    }
                }
            }
            return View("posts_reply");
        }

        bool Verification()
        {

            Verify vf = new Verify();
            vf.VerificationMessage = VerificationMessage;

            VerifyField vContents = vf.NewVerifyField(Request.Form["Contents"], "Contents");
            vContents.AddNull("请输入内容");
            vf.Begin();
            return vf.IsVerify;
        }
    }
}
